# -*- coding: utf-8 -*-

from __future__ import with_statement

from unittest import TestCase
from google.appengine.ext import testbed

from dicthash.for_testing import TestFlask, TestGoogleDataStore
from werkzeug.exceptions import NotFound

from user.code.views import _view_user_data, User


from user.code.backends.working import models
models.USER_NAME_VALIDATOR = lambda name: True


class MyFlask(TestFlask):
    """ """
    TEMPLATES = {
        'user/view_user_data.html': """ 
            %user/view_user_data.html%
        """
    }
    
    
class TestBase(TestGoogleDataStore):
    """ """
    def setUp(self):
        """ """
        super(TestBase, self).setUp()
        
        self.app = MyFlask(__name__)
        self.app.config.update(
            SECRET_KEY = '12345'
        )
       
        
class TestViewUser(TestBase):
    """ """
    def test_user_not_found(self):
        """ """
        with self.app.test_request_context('/'):
            try:
                _view_user_data('foo')
            except NotFound:
                pass
            else:
                self.assertTrue(False)
                
    def test_ok(self):
        """ """
        User.new('foo', 'foo@gmail.com').put()
        
        with self.app.test_request_context('/'):
            res = _view_user_data('foo')
            self.assertEqual(res, dict(user_in_question={'name': 'foo', 'openid': 'foo@gmail.com'}))